/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Globalization;

namespace DaveSexton.DocProject.Sandcastle
{
	[Serializable]
	public sealed class BuildComponentsLoader : AssemblyLoader
	{
		#region Public Properties
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
		public static readonly BuildComponentsLoader Instance = new BuildComponentsLoader();

		public static string AssemblyFileName
		{
			get
			{
				return assemblyFileName;
			}
		}
		#endregion

		#region Private / Protected
		private const string assemblyFileName = "BuildComponents.dll";
		private string path;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="BuildComponentsLoader" /> class.
		/// </summary>
		private BuildComponentsLoader()
		{
		}
		#endregion

		#region Methods
		/// <remarks>
		/// This method should only be used once, for the first time that <see cref="Instance"/>
		/// will be used in a new <see cref="AppDomain"/>.
		/// </remarks>
		public static bool TryLoadAssembly(string path)
		{
			BuildComponentsLoader instance = Instance;
			instance.path = path;
			return instance.TryLoadAssembly();
		}

		protected override System.Reflection.AssemblyName CreateAssemblyName()
		{
			return AssemblyName.GetAssemblyName(System.IO.Path.Combine(
				path ?? SandcastleBuildEngineProvider.Paths.Tools, assemblyFileName));
		}

		protected override bool LoadAssembly(AssemblyName name)
		{
			if (base.LoadAssembly(name))
				// BuildComponents.dll depends on BuildAssemblerLibrary.dll
				return BuildAssemblerLoader.Instance.TryLoadAssembly();

			return false;
		}
    #endregion
  }
}
